package sk.vasicek.drinkagent.activity;

import java.util.ArrayList;
import java.util.HashMap;

import sk.vasicek.drinkagent.R;
import sk.vasicek.drinkagent.domain.CustomDrink;
import sk.vasicek.drinkagent.service.BackgroundAdapter;
import sk.vasicek.drinkagent.service.CustomDrinkHelper;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;

public class CustomDrinksActivity extends Activity {

	private static String TAG = "DrinkPlacesActivity";

	protected ListView drinkPointList;
	protected ArrayAdapter<String> adapter;
	protected ArrayList<String> drinkPointsList;
	protected HashMap<String, CustomDrink> drinkPointsMap;

	protected ImageButton settingsButton;
	protected ImageButton mapButton;
	protected ImageButton placesButton;
	protected ImageButton drinksButton;

	protected CustomDrinkHelper drinkDatabaseHelper;
	SharedPreferences prefs;

	private ImageButton addCustomDrink;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.drink_places);
		Log.d(TAG, "Started DrinkPlacesActivity");
		prefs = PreferenceManager.getDefaultSharedPreferences(this);
		BackgroundAdapter.updateBackgroundFromPrefs(prefs, this);

		updateCustomDrinks();
		this.drinkPointList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapterView, View view, int no, long arg3) {
				// Toast toast = Toast.makeText(getApplicationContext(),
				// "You picked " + contactPhoneList.get(no),
				// Toast.LENGTH_SHORT);
				// toast.show();
				Intent intent = new Intent(CustomDrinksActivity.this, CustomDrinkDetailActivity.class);
				CustomDrink drinkPoint = drinkPointsMap.get(drinkPointsList.get(no));
				if (drinkPoint.getId() != null) {
					intent.putExtra(CustomDrink.DRINK_ID, drinkPoint.getId());
				}
				if (drinkPoint.getName() != null && !"".equals(drinkPoint.getName())) {
					intent.putExtra(CustomDrink.DRINK_NAME, drinkPoint.getName());
				}
				if (drinkPoint.getNote() != null && !"".equals(drinkPoint.getNote())) {
					intent.putExtra(CustomDrink.DRINK_NOTE, drinkPoint.getNote());
				}
				if (drinkPoint.getVolume() != 0d) {
					intent.putExtra(CustomDrink.VOLUME, drinkPoint.getVolume());
				}
				if (drinkPoint.getConcentration() != 0d) {
					intent.putExtra(CustomDrink.ALCOHOL_CONCENTRATION, drinkPoint.getConcentration());
				}
				startActivity(intent);
			}

		});

		this.mapButton = (ImageButton) this.findViewById(R.id.map);
		mapButton.setVisibility(View.VISIBLE);
		this.mapButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getApplicationContext(), DrinkMapsActivity.class);
				startActivity(intent);
			}
		});
		this.placesButton = (ImageButton) this.findViewById(R.id.places);
		placesButton.setVisibility(View.VISIBLE);
		this.placesButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// Toast.makeText(getApplicationContext(),
				// getResources().getText(R.string.alreadyToast),
				// Toast.LENGTH_SHORT).show();

				Intent intent = new Intent(getApplicationContext(), DrinkPlacesActivity.class);
				startActivity(intent);
			}
		});

		this.settingsButton = (ImageButton) this.findViewById(R.id.settings);
		settingsButton.setVisibility(View.VISIBLE);
		this.settingsButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getApplicationContext(), SettingsActivity.class);
				startActivity(intent);
			}
		});

		this.drinksButton = (ImageButton) this.findViewById(R.id.drinks);

		this.drinksButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getApplicationContext(), DrinkManagerListActivity.class);
				startActivity(intent);
				// Toast.makeText(getApplicationContext(),
				// getResources().getText(R.string.alreadyToast),
				// Toast.LENGTH_SHORT).show();

			}
		});
		if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
			placesButton.setVisibility(View.GONE);
			mapButton.setVisibility(View.INVISIBLE);
			this.addCustomDrink = (ImageButton) this.findViewById(R.id.add_custom_drink);
			addCustomDrink.setVisibility(View.VISIBLE);
			this.addCustomDrink.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent drinkPointIntent = new Intent(CustomDrinksActivity.this, CustomDrinkDetailActivity.class);
					CustomDrink customDrink = new CustomDrink(0.16, 0.2, "", "");
					CustomDrinkHelper drinkHelper = new CustomDrinkHelper(CustomDrinksActivity.this);
					String name = customDrink.getName();
					if (name == null || "".equals(name.trim())) {
						name = getResources().getText(R.string.drinkLabelGeneric).toString();
						customDrink.setName(name);
					}
					long drinkPointId = drinkHelper.insertCustomDrink(customDrink);
					if (drinkPointId != -1) {
						drinkPointIntent.putExtra(CustomDrink.DRINK_ID, (int) drinkPointId);

						if (customDrink.getName() != null && !"".equals(customDrink.getName())) {
							drinkPointIntent.putExtra(CustomDrink.DRINK_NAME, customDrink.getName());
						}
						if (customDrink.getNote() != null && !"".equals(customDrink.getNote())) {
							drinkPointIntent.putExtra(CustomDrink.DRINK_NOTE, customDrink.getNote());
						}
						if (customDrink.getVolume() != 0d) {
							drinkPointIntent.putExtra(CustomDrink.VOLUME, customDrink.getVolume());
						}
						if (customDrink.getConcentration() != 0d) {
							drinkPointIntent.putExtra(CustomDrink.ALCOHOL_CONCENTRATION, customDrink.getConcentration());
						}
						// startActivity(drinkPointIntent);
						CustomDrinksActivity.this.startActivityForResult(drinkPointIntent, 0);
					}

				}
			});
		}
	}

	private void updateCustomDrinks() {
		drinkPointsList = new ArrayList<String>();
		drinkPointsMap = new HashMap<String, CustomDrink>();

		ArrayList<CustomDrink> drinkPoints = new ArrayList<CustomDrink>();

		drinkDatabaseHelper = new CustomDrinkHelper(this);
		if (drinkDatabaseHelper != null) {
			try {
				drinkPoints = drinkDatabaseHelper.getAllCustomDrinks();
			} catch (Exception ex) {
				Log.e(TAG, "Failed to read drink points", ex);
			}
			for (CustomDrink drinkPoint : drinkPoints) {
				drinkPointsMap.put(drinkPoint.getId() + ": " + drinkPoint.getName(), drinkPoint);
				drinkPointsList.add(drinkPoint.getId() + ": " + drinkPoint.getName());
			}
		}
		this.drinkPointList = (ListView) findViewById(R.id.drinkPlaceList);
		adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.drink_list_item, drinkPointsList);
		drinkPointList.setAdapter(adapter);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = this.getMenuInflater();
		inflater.inflate(R.menu.custom_drinks_optionmenu, menu);
		MenuItem settings = menu.findItem(R.id.settings);
		settings.setIntent(new Intent(this, SettingsActivity.class));
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
		case R.id.exit:
			CustomDrinksActivity.this.finish();
			return true;
		case R.id.addcustom:
			Intent drinkPointIntent = new Intent(CustomDrinksActivity.this, CustomDrinkDetailActivity.class);
			CustomDrink customDrink = new CustomDrink(0.16, 0.2, "", "");
			CustomDrinkHelper drinkHelper = new CustomDrinkHelper(this);
			long drinkPointId = drinkHelper.insertCustomDrink(customDrink);
			if (drinkPointId != -1) {
				drinkPointIntent.putExtra(CustomDrink.DRINK_ID, (int) drinkPointId);

				if (customDrink.getName() != null && !"".equals(customDrink.getName())) {
					drinkPointIntent.putExtra(CustomDrink.DRINK_NAME, customDrink.getName());
				}
				if (customDrink.getNote() != null && !"".equals(customDrink.getNote())) {
					drinkPointIntent.putExtra(CustomDrink.DRINK_NOTE, customDrink.getNote());
				}
				if (customDrink.getVolume() != 0d) {
					drinkPointIntent.putExtra(CustomDrink.VOLUME, customDrink.getVolume());
				}
				if (customDrink.getConcentration() != 0d) {
					drinkPointIntent.putExtra(CustomDrink.ALCOHOL_CONCENTRATION, customDrink.getConcentration());
				}
				// startActivity(drinkPointIntent);
				this.startActivityForResult(drinkPointIntent, 0);
			}
			return true;
		case R.id.settings:
			Intent settingsIntent = item.getIntent();
			this.startActivityForResult(settingsIntent, 0);
			return true;
		case android.R.id.home:
			// Toast.makeText(DrinkManagerListActivity.this.getApplicationContext(),
			// getResources().getText(R.string.alreadyToast),
			// Toast.LENGTH_SHORT).show();
			Intent intent = new Intent(this, DrinkManagerListActivity.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			return true;
		case R.id.drinks:
			Intent drinksIntent = new Intent(getApplicationContext(), DrinkManagerListActivity.class);
			this.startActivityForResult(drinksIntent, 0);
			return true;
		case R.id.places:
			Intent placesIntent = new Intent(this, DrinkPlacesActivity.class);
			this.startActivityForResult(placesIntent, 0);

			// Toast.makeText(getApplicationContext(),
			// getResources().getText(R.string.alreadyToast),
			// Toast.LENGTH_SHORT).show();
			return true;
		case R.id.custom:
			// Intent customIntent = new Intent(this,
			// CustomDrinksActivity.class);
			// this.startActivityForResult(customIntent, 0);
			Toast.makeText(getApplicationContext(), getResources().getText(R.string.alreadyToast), Toast.LENGTH_SHORT).show();
			return true;
		case R.id.history:
			Intent historyIntent = new Intent(this, DrinkHistoryActivity.class);
			this.startActivityForResult(historyIntent, 0);
			// Toast.makeText(getApplicationContext(),
			// getResources().getText(R.string.alreadyToast),
			// Toast.LENGTH_SHORT).show();
			return true;
		case R.id.graphs:
			Intent graphsIntent = new Intent(this, DrinkGraphActivity.class);
			this.startActivityForResult(graphsIntent, 0);
			return true;
		case R.id.back:
			Intent backIntent = new Intent(getApplicationContext(), BackgroundChooserActivity.class);
			this.startActivityForResult(backIntent, 0);
			return true;
		case R.id.map:
			Intent mapsIntent = new Intent(getApplicationContext(), DrinkMapsActivity.class);
			this.startActivityForResult(mapsIntent, 0);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	protected void onResume() {
		updateCustomDrinks();
		super.onResume();
		BackgroundAdapter.updateBackgroundFromPrefs(prefs, this);
	}
}
